package com.lanou3g.musicenjoy.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import com.lanou3g.musicenjoy.activities.NewActivity;
import com.lanou3g.musicenjoy.entity.New;
import com.lanou3g.musicenjoy.adapter.NewAdapter;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class HotFragment extends Fragment implements AdapterView.OnItemClickListener {
    private List<New.ResultEntity.MvListEntity> datas;
    private NewAdapter adapter;
    private RequestQueue queue;
    private GridView gridView;
    private View view;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_new,null);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        queue= VolleySingleton.getInstance(getActivity()).getRequestQueue();
        adapter=new NewAdapter(getActivity());
        gridView= (GridView) view.findViewById(R.id.newGrideView);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);

        String url="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=" +
                "baidu.ting.mv.searchMV&format=json&order=0&page_num=1&page_size=20&query=%E5%85%A8%E9%83%A8";
        StringRequest request=new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response!=null){
                    Gson gson=new Gson();
                    New result=gson.fromJson(response,New.class);
                    if (result!=null){
                        datas=result.getResult().getMv_list();
                        adapter.initData(datas);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(getActivity(), NewActivity.class);
        String a=datas.get(position).getMv_id();
        intent.putExtra("a", a);
        startActivity(intent);
    }
}
